package com.fg.common.util;

import cn.hutool.core.bean.BeanUtil;
import com.fg.common.constant.Constant;
import com.fg.common.entity.Dto.UserDto;
import com.fg.common.exception.FgException;
import com.fg.common.response.R;
import com.fg.common.response.ResponseCodeConstant;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author lv
 * @version 1.0.0
 */
@Slf4j
public class UserUtil {

    private UserUtil(){}

    public static final ThreadLocal<UserDto> user = new ThreadLocal();

    /**
     * 获取当前请求用户
     * @return 当前请求用户
     */
    public static UserDto getCurrentUser(){
        UserDto userDTO = user.get();
        Assert.assertNotNull(userDTO, "获取用户失败");
        return userDTO;
    }

    /**
     * 获取当前请求用户id
     * @return 请求用户id
     */
    public static String getCurrentUserId(){
        String id = getCurrentUser().getUserId();
        Assert.assertNotNull(id, "获取用户id失败");
        return id;
    }

    /**
     * 获取当前请求token
     * @return token
     */
    public static String getToken(){
        String token = getCurrentUser().getToken();
        Assert.assertNotNull(token, "获取当前请求token失败");
        return token;
    }

    /**
     * 将用户模块接口响应数据转换为UserDto对象
     * @param json 用户模块响应的json
     * @return UserDto
     */
    public static UserDto getUser(String json){
        R r = getR(json);
        UserDto userDto = BeanUtil.copyProperties(Constant.GSON.fromJson(Constant.GSON.toJson(r.getData()), HashMap.class),
                UserDto.class);
        userDto.setUserId(String.valueOf(new BigDecimal(userDto.getUserId()).intValue()));
        userDto.setCreateUserId(String.valueOf(new BigDecimal(userDto.getCreateUserId()).intValue()));
        return userDto;
    }

    /**
     * 将用户模块接口响应数据转换为UserDto对象列表
     * @param json 接口返回的json字符串
     * @return UserDto对象列表
     */
    public static List<UserDto> getUserList(String json){
        R r = getR(json);
        List<UserDto> userDtoList = Constant.GSON.fromJson(Constant.GSON.toJson(r.getData()),
                TypeToken.getParameterized(List.class, UserDto.class).getType());
        userDtoList.stream().forEach(userDto -> {
            userDto.setUserId(String.valueOf(new BigDecimal(userDto.getUserId()).intValue()));
            userDto.setCreateUserId(String.valueOf(new BigDecimal(userDto.getCreateUserId()).intValue()));
        });
        return userDtoList;
    }

    /**
     * 将用户模块接口响应数据转换为R对象
     * @param json 用户模块响应的json
     * @return R
     */
    public static R getR(String json){
        if (StringUtils.isBlank(json)){
            throw new FgException(ResponseCodeConstant.REMOTE_SERVICE_USER_ERROR, "用户模块响应用户数据格式错误:"+json);
        }
        HashMap map = Constant.GSON.fromJson(json, HashMap.class);
        Object code = map.get("code");
        if (code == null || !Objects.equals(code, ResponseCodeConstant.SUCCESS)){
            throw new FgException(ResponseCodeConstant.REMOTE_SERVICE_USER_ERROR, "用户模块响应用户数据格式错误:"+json);
        }
        R r = BeanUtil.copyProperties(map, R.class);
        Object data = r.getData();
        if (data == null){
            throw new FgException(ResponseCodeConstant.REMOTE_SERVICE_USER_ERROR, "用户模块响应用户数据格式错误:"+json);
        }
        return r;
    }
}
